package com.qf.controller;

import com.qf.pojo.User;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @author sunyamei
 * @version V1.0
 * @Project springmvc_Demo
 * @Package com.qf.controller
 * @Description:
 * @Date 2022/7/14 20:58
 *
 * @RequestBody:
 *  *           使用时机，异步请求，请求体传参（Post）参数类型json
 *  *           作用：可以把请求体中的json数据转为java对象
 *  * @ResponseBody:
 *  *           使用时机：异步请求
 *  *           作用：可以把方法的返回值按照json格式返回前端
 *  *                 字符串原样返回，对象或集合转为json返回
 *  *
 *  *          @RestController 相当于@Controller+@ResponseBody
 */
 @RequestMapping("/users")
@RestController
@CrossOrigin("http://127.0.0.1:8020")
public class UserController {

    /**
     * 获取所有用户信息
     * @return
     */
    @GetMapping("")
    public List<User> findAll(){
        List<User> users=new ArrayList<User>();
        users.add(new User(1,"tom","123456","admin"));
        users.add(new User(2,"jack","123456","admin"));
        users.add(new User(3,"amy","123456","admin"));
        return users;
    }
    //PathVariable把路径中的id数据赋值给参数id
    @GetMapping("/{id}")
    public User findById(@PathVariable("id") Integer id){
        System.out.println("id = " + id);
        return new User(1,"tom","123456","admin");
    }

    @DeleteMapping("/{id}")
    public String deleteById(@PathVariable("id") Integer id){
        System.out.println("id = " + id);
        return "success";
    }

    @PutMapping("")
    public String update(@RequestBody User user){
        System.out.println("user = " + user);
        return "success";
    }

    @PostMapping("")
    public String insert(@RequestBody User user){
        System.out.println("user = " + user);
        return "success";
    }











//    @PostMapping("/testPost")
//    public String testPost(@RequestBody User user){
//        System.out.println("user = " + user);
//        return "success";//json字符串
//    }
//    @GetMapping("/testGet")
//    public User testGet(){
//        User user=new User();
//        user.setUsername("tom");
//        user.setPassword("888");
//        user.setNickname("admin");
////        user.setBirthday(new Date());
////        user.setMoney(100);
//        return user;
//    }


}
